package com.push.notifications;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;



public class CUtilsFunction {
	
	 public final static String DEFAULT_SERVER = "http://vipmobi.net:7001/MOBILE/ServiceJsonService?wsdl";
	 public final static String DOWNLOAD_SERVER = "http://vipmobi.net:7001/MOBILE/download?";
	 public final static String DEFAULT_USER = "push";
	 public final static String APP_NAME = "PushNotification";
	
	 /**==============================================***/
	  /** convert Date to String by hour:day/month/year***/
	  /** @Author : thien_vv 22/11/2012				   ***/
	  /**==============================================***/
		public static String getDate()
	  {
		  Date date = new Date();	
		  int minutes = date.getMinutes();
		  int nHours = date.getHours();
		  int nDay = date.getDay();
		  int nMonth = date.getMonth();
		  int Year = date.getYear() + 1900;
		  String sAdd = " AM - ";
		  if(nHours >=12)
			  sAdd = " PM - ";
		  
		  String sDate = nHours +":"+ minutes +sAdd +nDay + "/"+nMonth+"/"+Year;
		  return sDate;
	  }
		
		
		public static boolean checkConnect(Context context)
		 {
			 ConnectivityManager cm =
				        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
				    NetworkInfo netInfo = cm.getActiveNetworkInfo();
				    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
				        return true;
				    }
				    return false;
		 }
		
		
		public static String updateVersion(String currentVersion, String imei) {
			String server = DEFAULT_SERVER;
			try {
				URL u = new URL(server);
				URLConnection uc = u.openConnection();
				HttpURLConnection connection = (HttpURLConnection) uc;
				connection.setUseCaches(false);
				connection.setRequestProperty("Connection", "keep-alive");
				connection.setDoOutput(true);
				connection.setDoInput(true);
				connection.setConnectTimeout(30000);
				String xml = "<soap:Envelope "
						+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
						+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
						+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
						+ "  <soap:Body> \r\n"
						+ " <deviceConnect xmlns='http://JsonUtils/'> \r\n"
						+ "	<deviceImei  xmlns=''>" + imei
						+ "</deviceImei> \r\n"
						+ "	<appName  xmlns=''>" + APP_NAME + "</appName> \r\n"
						+ "	<OS  xmlns=''>" + "Android" + "</OS > \r\n"
						+ "	<currentVersion  xmlns=''>" + currentVersion
						+ "</currentVersion > \r\n"
						+ " <username xmlns=''>"+ DEFAULT_USER + "</username> \r\n"	
						+ " <checkVersion xmlns=''>true</checkVersion> \r\n"
						+ " <addExpiredDate xmlns=''/> \r\n"	
						+ "    </deviceConnect> \r\n" + "  </soap:Body> \r\n"
						+ "</soap:Envelope> \r\n";
				connection.setRequestMethod("POST");
				connection.setRequestProperty("soapAction", "");
				connection.setRequestProperty("Content-Type",
						"text/xml;charset=UTF-8");
				int len = xml.length();
				connection.setRequestProperty("Content-Length",
						Integer.toString(len));
				connection.connect();
				OutputStream out = connection.getOutputStream();
				OutputStreamWriter wout = new OutputStreamWriter(out);
				wout.write(xml);
				wout.flush();
				wout.close();

				StringBuffer response = new StringBuffer();
				InputStream inStream = connection.getInputStream();
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						inStream));
				String line;
				while ((line = rd.readLine()) != null) {
					response.append(line);
					String sReturn = response.toString();
					return sReturn;
				}
			} catch (IOException e) {
				return "";
			}
			return "";

		}
		
		
		
}
